package com.gko100.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Category;
import com.gko100.model.Disease;
import com.gko100.model.Language;


public class DiseaseAction extends AdminBaseAction {
	
	private Disease disease;
	private List<Disease> disList;
	private Map<Disease, List<Disease>> disMap;
	public Disease getDisease() {
		return disease;
	}
	public void setDisease(Disease disease) {
		this.disease = disease;
	}
	public List<Disease> getDisList() {
		return disList;
	}
	public void setDisList(List<Disease> disList) {
		this.disList = disList;
	}
	public Map<Disease, List<Disease>> getDisMap() {
		return disMap;
	}
	public void setDisMap(Map<Disease, List<Disease>> disMap) {
		this.disMap = disMap;
	}



	private Category cate;
	
	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}

	private String[] selectLangId;
	private Language lang;
	private List<Language> langList;
	
	
	public String[] getSelectLangId() {
		return selectLangId;
	}
	public void setSelectLangId(String[] selectLangId) {
		this.selectLangId = selectLangId;
	}
	public Language getLang() {
		return lang;
	}
	public void setLang(Language lang) {
		this.lang = lang;
	}
	public List<Language> getLangList() {
		return langList;
	}
	public void setLangList(List<Language> langList) {
		this.langList = langList;
	}
	
	private String paramDisPid;
	
	public String getParamDisPid() {
		return paramDisPid;
	}
	public void setParamDisPid(String paramDisPid) {
		this.paramDisPid = paramDisPid;
	}
	
	
	public String find_disListByDisPid() throws Exception {
		
		
		if(this.getParamDisPid()!=null){
			
			int disPid = Integer.parseInt(this.getParamDisPid());
			List<Disease> disList = adminManager.findDisListByDisPid(disPid);
			if(disList!=null&&disList.size()>=1){
				
				Map<Disease, List<Disease>> dMap = new LinkedHashMap<Disease, List<Disease>>();
				for(int i=0;i<disList.size();i++){
					
					List<Disease> dList = adminManager.findDisListByDisPid(disList.get(i).getDiseaseId());
					if(dList!=null&&dList.size()>=1){
						
						dMap.put(disList.get(i), dList);
					}else{
						dMap.put(disList.get(i), null);
					}					
				}
				
				this.setDisMap(dMap);
				this.setParamDisPid(null);
				this.setDisease(null);
				
			}
			return "disListByDisPid_find_success";
			
		}
		if(this.getDisease()!=null&&this.getDisease().getDisPid()!=null){
			
			
			List<Disease> disList = adminManager.findDisListByDisPid(this.getDisease().getDisPid());
			if(disList!=null&&disList.size()>=1){
				
				Map<Disease, List<Disease>> dMap = new LinkedHashMap<Disease, List<Disease>>();
				for(int i=0;i<disList.size();i++){
					
					List<Disease> dList = adminManager.findDisListByDisPid(disList.get(i).getDiseaseId());
					if(dList!=null&&dList.size()>=1){
						
						dMap.put(disList.get(i), dList);
					}else{
						dMap.put(disList.get(i), null);
					}					
				}
				
				this.setDisMap(dMap);
				this.setDisease(null);
			}
			return "disListByDisPid_find_success";
		}
		
		return "back_error";

	}
	
	public String insert_disease() throws Exception {
		
		if(this.getDisease()!=null&&this.getDisease().getDisPid()!=null){
			
			List<Language> langList = adminManager.findLangListAll();
			if(langList!=null&&langList.size()>=1){
				this.setLangList(langList);
			}
		}else if(this.getDisease()!=null&&this.getDisease().getDiseaseId()!=null){
			Disease disease = adminManager.findDisByDisId(this.getDisease().getDiseaseId());
			if(disease!=null){
				
				disease.setDisPid(disease.getDiseaseId());
				this.setDisease(disease);
			}
		}
		
		
		return "disease_insert_success";
	}
	
	public String add_disease() throws Exception {
		
		if(this.getSelectLangId()!=null){
			System.out.println("this.getSelectLangId()==================="+this.getSelectLangId()[0]);
			Language lang = adminManager.findLangByLangId(Integer.parseInt(this.getSelectLangId()[0]));
			this.getDisease().setLang(lang);
			
		}else{
			
			Disease disease = adminManager.findDisByDisId(this.getDisease().getDisPid());
			Language lang = disease.getLang();
			this.getDisease().setLang(lang);
			
		}
		
		
		adminManager.add_disease(this.getDisease());
		
		this.setDisease(null);
		
		return "disease_add_success";
	}
	
	public String del_disease() throws Exception {
		
		if(this.getDisease().getDiseaseId()!=null){
			
			Disease disease = adminManager.findDisByDisId(this.getDisease().getDiseaseId());
			if(disease!=null){
				
				List<Disease> disList = adminManager.findDisListByDisPid(disease.getDiseaseId());
				if(disList!=null&&disList.size()>=1){
					for(int i=0;i<disList.size();i++){
						adminManager.del_disease(disList.get(i));
					}
				}
				adminManager.del_disease(disease);
			}
		}
		
		return "disease_del_success";
	}
	
	public String upload_disease() throws Exception {
		
		if(this.getDisease().getDiseaseId()!=null){
			
			Disease disease = adminManager.findDisByDisId(this.getDisease().getDiseaseId());
			
			if(disease!=null){
				this.setDisease(disease);
			}
		}

		return "disease_upload_success";
	}
	
	public String modify_disease() throws Exception {
		
		if(this.getDisease()!=null){
			System.out.println(this.getDisease().getSummary());
			System.out.println(this.getDisease().getUrl());
			System.out.println(this.getDisease().getContent());
			adminManager.modify_disease(this.getDisease());
			this.setDisease(null);
			return "disease_modify_success";
		}
		
		return "back_error";
	}
	
	
}
